package com.gf.exam.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gf.base.controller.BasicController;
import com.gf.base.type.ExamType;
import com.gf.base.type.PlayMode;
import com.gf.base.type.Role;
import com.gf.common.bean.DataRequest;
import com.gf.common.bean.DataResponse;
import com.gf.common.bean.Page;
import com.gf.common.constant.Constants;
import com.gf.common.constant.ViewPaths;
import com.gf.common.util.WebHelper;
import com.gf.event.service.EventService;
import com.gf.exam.service.ExamCellService;
import com.gf.user.model.User;

@Controller
@RequestMapping("/exam")
public class ExamController extends BasicController {

	private static final Log log = LogFactory.getLog(ExamController.class);

	@Autowired
	private ExamCellService examCellService;

	@Autowired
	private EventService eventService;

	/**
	 * 登入测试项目发布页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/make", method = RequestMethod.GET)
	public ModelAndView inExamIndex() {
		User user = WebHelper.getSessionUser();
		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.EXAM_MAKE_VIEW);
		return mvc;
	}

	/**
	 * 保存项目<br>
	 * 对于PPT 批量保存
	 * 
	 * @param title
	 * @param introduction
	 * @param cellNo
	 * @param exTimerH
	 * @param exTimerM
	 * @param exTimerS
	 * @param examType
	 * @param htmlContents
	 * @param textContents
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody
	long saveExam(
			String title,
			String introduction,
			int cellNo,
			boolean active,
			int repeatFrom,
			int repeatTo,
			String playMode,
			int repeatTime,
			int exTimerH,
			int exTimerM,
			int exTimerS,
			String examType,
			@RequestParam(value = "htmlContents[]", required = true) String[] htmlContents) {
		User user = WebHelper.checkAuthorityOfPublish();

		log.info("save exam--->title：" + title + " introduction:"
				+ introduction + " cellNo:" + cellNo + " exTimerH:" + exTimerH
				+ " exTimerM:" + exTimerM + " exTimerS:" + exTimerS
				+ " examType:" + examType);

		log.info("htmlContents:" + htmlContents[0]);

		return examCellService.save(user, title, introduction, cellNo, active,
				repeatFrom, repeatTo, PlayMode.valueOf(playMode), repeatTime,
				exTimerH, exTimerM, exTimerS, examType, htmlContents);

	}

	/**
	 * 更新项目信息及所有PPT
	 * 
	 * @param id
	 * @param title
	 * @param introduction
	 * @param cellNo
	 * @param exTimerH
	 * @param exTimerM
	 * @param exTimerS
	 * @param examType
	 *            "private"
	 * @param htmlContents
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	long update(
			long id,
			String title,
			String introduction,
			int cellNo,
			boolean active,
			int repeatFrom,
			int repeatTo,
			String playMode,
			int repeatTime,
			int exTimerH,
			int exTimerM,
			int exTimerS,
			@RequestParam(value = "htmlContents[]", required = true) String[] htmlContents) {

		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return -1;
		}
		
		return examCellService.update(id, title, introduction, cellNo, active,
				repeatFrom, repeatTo, PlayMode.valueOf(playMode), repeatTime,
				exTimerH, exTimerM, exTimerS, htmlContents);

	}

	/**
	 * 发布项目<br>
	 * 能被搜索得到
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/publish", method = RequestMethod.POST)
	public @ResponseBody
	long publish(long id, String title) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return -1;
		}
		// 生成发布事件
		eventService.publishProjectEvent(user.getId(), user.getNickname(),
				title, id);

		return examCellService.publish(id);
	}

	/**
	 * 测试首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public ModelAndView index() {
		User user = WebHelper.getSessionUser();
		ModelAndView mvc = new ModelAndView();
		mvc.addObject("user", user);
		mvc.setViewName(ViewPaths.EXAM_INDEX_VIEW);
		return mvc;
	}

	/**
	 * 得到我参与的所有问卷列表 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/participate", method = RequestMethod.GET)
	public ModelAndView participateView() {
		WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.PARTICIPAGE_VIEW);

		return mvc;
	}

	/**
	 * 浏览别的用户主页时：得到该用户参与的所有问卷列表 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/otherParticipate", method = RequestMethod.GET)
	public ModelAndView otherPrticipateView() {
		WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.OTHER_PARTICIPAGE_VIEW);

		return mvc;
	}

	/**
	 * 得到我发布的所有问卷列表 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/releasedOf", method = RequestMethod.GET)
	public ModelAndView releasedOfView() {
		WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.RELEASE_OF_VIEW);

		return mvc;
	}
	/**
	 * 得到我发布的所有问卷列表 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/unReleasedOf", method = RequestMethod.GET)
	public ModelAndView unReleasedView() {
		WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.UNRELEASEDOF_VIEW);

		return mvc;
	}
	/**
	 * 浏览别的用户主页时：得到该用户发布的所有问卷列表 页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/otherReleasedOf", method = RequestMethod.GET)
	public ModelAndView otherReleasedOfView() {
		WebHelper.getSessionUser();

		ModelAndView mvc = new ModelAndView();
		mvc.setViewName(ViewPaths.OTHER_RELEASE_OF_VIEW);

		return mvc;
	}

	/**
	 * 得到我参与的所有问卷列表
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/participateData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse participate(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = examCellService.getParticipate(user.getId(), curpage,
				pageSize);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

	/**
	 * 浏览别的用户主页时：得到该用户参与的所有问卷列表
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/otherParticipateData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse otherParticipate(DataRequest dataRequest, long otherId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}
		
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = examCellService.getParticipate(otherId, curpage, pageSize);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

	/**
	 * 得到我发布的所有问卷列表
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/releasedOfData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse releasedOf(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null || user.getRole().equals(Role.NORMAL)) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = examCellService.getMyExam(user.getId(), curpage,
				pageSize,ExamType.PUBLIC);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}
	
	@RequestMapping(value = "/unReleasedOfData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse unReleasedOf(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null || user.getRole().equals(Role.NORMAL)) {
			return new DataResponse();
		}

		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = examCellService.getMyExam(user.getId(), curpage,
				pageSize,ExamType.PRIVATE);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

	/**
	 * 浏览别的用户主页时：得到该用户发布的所有问卷列表
	 * 
	 * @param dataRequest
	 * @return
	 */
	@RequestMapping(value = "/otherReleasedOfData", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse otherReleasedOf(DataRequest dataRequest, long otherId) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}
		
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = examCellService.getMyExam(otherId, curpage, pageSize,ExamType.PUBLIC);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;

	}

	@RequestMapping(value = "/getAll", method = RequestMethod.POST)
	public @ResponseBody
	DataResponse getAll(DataRequest dataRequest) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return new DataResponse();
		}
		
		int curpage = dataRequest.getPage();
		int pageSize = dataRequest.getRows();

		Page page = examCellService.getAll(pageSize, curpage);
		DataResponse dataResponse = new DataResponse();

		dataResponse.setResult(page.getResults());
		dataResponse.setPage(page.getCurpage());
		dataResponse.setRecords(page.getTotal());
		dataResponse.setTotal(page.getTotalPage());

		return dataResponse;
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	boolean delete(long id) {
		User user = WebHelper.getSessionAttribute(Constants.SESSION_USER);
		if (user == null) {
			return false;
		}
		
		return examCellService.delete(id);
	}
}
